<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键" v-if="false"></el-input>
			</el-form-item>

			<el-form-item label="职级编码" prop="code">
				<el-input v-model="dataForm.code" placeholder="请输入"></el-input>
			</el-form-item>
			<el-form-item label="职级名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="请输入"></el-input>
			</el-form-item>
			<el-form-item label="职等" prop="level">
				<input-number-range :disabled="false" :precision="1" v-model="dataForm.level" :minNum="0" :maxNum="101"
					:minStr="'最小值1'" :maxStr="'最大值100'" @changefrom="fnLevels" @changeto="fnLevele">
				</input-number-range>
			</el-form-item>

			<el-form-item label="职级描述" prop="description">
				<el-input v-model="dataForm.description" placeholder="请输入" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useLevelApi, useLevelSubmitApi } from '@/api/nb/organization/position/level'
import inputNumberRange from '@/components/input-number-range'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const fnInputfrom = ref()
const fnInputto = ref()

const dataForm = reactive({
	id: '',

	code: '',
	name: '',
	level: '',
	levelS: 0,
	levelE: 0,
	description: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getLevel(id)
	}
}

const getLevel = (id: number) => {
	useLevelApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		// fnLevel()
		useLevelSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

// export const inputfrom
const fnLevels = (data: number) => {
	dataForm.levelS = data
}

const fnLevele = (data: number) => {
	dataForm.levelE = data
}

defineExpose({
	init
})
</script>
